package com.kpijavatraining.module2.question;

import java.util.Arrays;

public class QuestionManyAnswers extends Question{


	/**
	 * 
	 */
	private static final long serialVersionUID = 5946824398032312089L;
	/**
	 * 
	 */
	private final String[] answers;
	private final Integer[] indexOfCorrectAnswer;
	
	public QuestionManyAnswers(String question, String[] answers , Integer[] indexOfCorrectAnswer) {
		super(question);
		this.answers = answers;
		this.indexOfCorrectAnswer = indexOfCorrectAnswer;
	}

	@Override
	public void answer(String answer){
		String[] stringResult = answer.split(" ");
		Integer[] result = new Integer[stringResult.length];		
		for(int i = 0 ; i < result.length ; i++){
			result[i] = Integer.valueOf(stringResult[i]);
		}
		Arrays.sort(result);
		Arrays.sort(getCorrectAnswer());
		if(Arrays.equals(result,getCorrectAnswer())){
			setScore(1);
		}
	}

	@Override
	public String toString(){
		StringBuffer string = new StringBuffer();
		String horizontalSeparator = "- ";
		String verticalSeparator = " | ";
		string.append(super.toString());
		string.append('\n');
		int index = 0;
		for(String answer : answers){
			string.append(String.format("%s %s %s %s", answer, horizontalSeparator, index, verticalSeparator));
			index++;
		}
		return string.toString();	
	}
	
	private Integer[] getCorrectAnswer() {
		return indexOfCorrectAnswer;
	}
}
